package com.sgb.kjwl.view.ui.activity.self.company

import android.content.Context
import android.content.Intent
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityAgreementBinding
import com.sgb.kjwl.di.self.DaggerSelfComponent
import com.sgb.kjwl.di.self.SelfModule
import com.sgb.kjwl.view.ui.fragment.manage.OtherInquiryFragment
import com.sgb.kjwl.view.ui.fragment.self.company.AgentAgreementFragment
import com.sgb.kjwl.viewmodel.self.company.AgreementViewModel
import com.swgk.core.base.di.AppComponent
import com.swgk.core.util.PickSelectEntity
import javax.inject.Inject

/**
 * 合同列表
 */
class AgentAgreementActivity : AppActivity(){

    private lateinit var mBinding : ActivityAgreementBinding
    @Inject
    lateinit var mViewModel : AgreementViewModel
    private lateinit var mAdapter : AgreementPageAdapter

    companion object{
        fun launch(context: Context){
            val intent  = Intent(context, AgentAgreementActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
        DaggerSelfComponent.builder().appComponent(appComponent)
            .selfModule(SelfModule()).build().inject(this)
    }

    override fun init() {
        super.init()
        mViewModel.getTitle()
    }

    override fun initView() {
        super.initView()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_agreement)
        mBinding.include.ivBack.setOnClickListener { finish() }
        mBinding.include.titleView.text = "合同列表"
        mViewModel.titles.observe(this, {
            if (it != null && it.isNotEmpty()){
                initTabLayout(it)
            }
        })
    }

    private fun initTabLayout(titles : List<PickSelectEntity<Any>>) {
        mAdapter = AgreementPageAdapter(supportFragmentManager, titles)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = titles.size
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        mBinding.viewPage.currentItem = 0
    }


    class AgreementPageAdapter(fm : FragmentManager, titles : List<PickSelectEntity<Any>>) : FragmentPagerAdapter(fm) {
        private var mTitles : List<PickSelectEntity<Any>> ?= null
        init {
            this.mTitles = titles
        }
        override fun getCount(): Int {
            if (mTitles != null){
                return mTitles!!.size
            }
            return 0
        }

        override fun getItem(position: Int): Fragment {
            if (mTitles == null) return OtherInquiryFragment.newInstance()
            if (position == 0){
                AgentAgreementFragment.newInstance(mTitles!![position].no)
            }
            return OtherInquiryFragment.newInstance()
        }

        override fun getPageTitle(position: Int): CharSequence {
            if (mTitles != null){
                return mTitles!![position].projectscale
            }
            return ""
        }


    }

}